package com.pactera.madp.cp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.pactera.madp.cp.api.em.beta.ChoreographyEnum;
import com.pactera.madp.cp.api.vo.beta.InfoMorningIndexVO;
import com.pactera.madp.cp.api.vo.beta.WorkChoreographyVO;
import com.pactera.madp.cp.api.vo.beta.WorkModuleAndNewsVO;
import com.pactera.madp.cp.api.vo.beta.WorkOriginalMidVO;
import com.pactera.madp.cp.service.InfoMorningService;
import com.pactera.madp.cp.service.WorkChoreographyService;
import com.pactera.madp.cp.service.WorkOriginalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @title 资讯早报-业务接口实现
 * @date 2022-06-15
 * @author Deming.Chang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InfoMorningServiceImpl implements InfoMorningService {

    /**
     * 精编资讯-业务服务
     */
    private final WorkChoreographyService choreographyService;
    /**
     * 原创资讯-业务服务
     */
    private final WorkOriginalService originalService;

    @Override
    public String getSummary() {
        WorkChoreographyVO morning = choreographyService.getLatestMorning();
        if (ObjectUtil.isNull(morning) || ObjectUtil.isNull(morning.getInfo())) {
            return StrUtil.EMPTY;
        }
        return morning.getInfo().getTitle();
    }

    @Override
    public InfoMorningIndexVO getIndex() {
        InfoMorningIndexVO vo = new InfoMorningIndexVO();
        // 1.0 获取最新早报数据
        WorkChoreographyVO morning = choreographyService.getLatestMorning();
        if(ObjectUtil.isNotNull(morning)){
            // 今日早报-基本信息
            InfoMorningIndexVO.Info info = InfoMorningIndexVO.Info.builder()
                    .key(morning.getNewsKey())
                    .title(morning.getInfo().getTitle())
                    .coverImg(morning.getInfo().getCoverImg())
                    .sendChat(morning.getInfo().getSendChat())
                    .build();
            vo.setInfo(info);
            // 今日早报-早报信息-获取第一个栏目数据
            vo.setHeadNews(this.coverNews(morning));
        }
        // 2.0 获取最近两条精编资讯数据
        List<WorkChoreographyVO> choreographyVOList = choreographyService.getLatestTwo();
        if(CollUtil.isNotEmpty(choreographyVOList)){
            List<List<InfoMorningIndexVO.News>> choreographyNews = new ArrayList<>();
            choreographyVOList.forEach(choreography -> {
                choreographyNews.add(this.coverNews(choreography));
            });
            vo.setChoreographyNews(choreographyNews);
        }

        // 3.0 获取最近两条策略研究（原创）数据
        List<WorkOriginalMidVO> beatOriginalList = originalService.getLatestTwoBeta();
        vo.setOriginalNews(this.coverOriginal(beatOriginalList));
        // 4.0 获取最近两条行内新闻（原创）数据
        List<WorkOriginalMidVO> bankOriginalList = originalService.getLatestTwoBank();
        vo.setBankNews(this.coverOriginal(bankOriginalList));
        return vo;
    }

    /**
     * 转换栏目新闻
     * @param choreography 待转换的资讯对象（含新闻栏目列表）
     * @return 转换后的新闻栏目
     */
    private List<InfoMorningIndexVO.News> coverNews(WorkChoreographyVO choreography){
        List<InfoMorningIndexVO.News> targets = new ArrayList<>();
        // 获取第一个栏目数据
        WorkModuleAndNewsVO moduleAndNewsVO = choreography.getModuleAndNews().get(0);
        if(CollUtil.isNotEmpty(moduleAndNewsVO.getNewsList())){
            moduleAndNewsVO.getNewsList().forEach(item -> {
                InfoMorningIndexVO.News target = InfoMorningIndexVO.News.builder()
                        .newsKey(choreography.getNewsKey())
                        .infoSubType(ChoreographyEnum.findByType(choreography.getType()).getName())
                        .title(item.getTitle())
                        .remark(item.getRemark())
                        .comment(item.getComment())
                        .build();
                targets.add(target);
            });
        }
        return targets;
    }

    /**
     * 转换策略研究|行内新闻对象
     * @param sources 待转换的对象
     * @return  转换后的对象
     */
    private List<InfoMorningIndexVO.Original> coverOriginal(List<WorkOriginalMidVO> sources){
        List<InfoMorningIndexVO.Original> targets = new ArrayList<>();
        if(CollUtil.isNotEmpty(sources)) {
            sources.forEach(source -> {
                List<String> tags = JSONUtil.toList(JSONUtil.parseArray(source.getNewsTags()), String.class);
                InfoMorningIndexVO.Original target = InfoMorningIndexVO.Original.builder()
                        .newsKey(source.getNewsKey())
                        .infoSubType(source.getInfoSubType())
                        .newsTitle(source.getNewsTitle())
                        .newsImg(source.getNewsImg())
                        .newsTags(tags)
                        .newsTime(source.getNewsTime())
                        .build();
                targets.add(target);
            });
        }
        return targets;
    }
}
